package controllers.advertisement;

import java.io.File;
import java.util.Date;

import models.Advertisement;
import models.Category;
import models.Comment;
import models.Product;
import models.User;
import play.Play;
import play.data.Form;
import play.mvc.Controller;
import play.mvc.Http.MultipartFormData;
import play.mvc.Http.MultipartFormData.FilePart;
import play.mvc.Result;

import com.avaje.ebean.Ebean;
import com.avaje.ebean.Junction;

import controllers.product.CommentController;
import controllers.user.UserController;

public class AdvertisementController extends Controller {

	static Form<Advertisement> advertisementForm = form(Advertisement.class);
	static Form<Comment> commentForm = form(Comment.class);

	public static Result index() {
		return ok(views.html.advertisement.test.render(" "));
		// TODO uj felulet betoltese .. talán mégsem.
	}

	public static Result search(String query) {
		String[] queries = query.split("\\s");

		Junction<Advertisement> junction = Advertisement.find.where()
				.disjunction();

		for (String expr : queries) {

			String searchExpression = "%" + expr + "%";

			junction.like("name", searchExpression).like("description",
					searchExpression);
		}
		return ok(views.html.advertisement.search.render(query, junction
				.endJunction().findList()));
	}

	public static Result list(Long id) {
		return ok(views.html.advertisement.list.render(Advertisement.find
				.where().eq("category_id", id).findList(),
				Ebean.find(Category.class, id)));
	}

	public static Result info(Long id) {
		return ok(views.html.advertisement.info.render(
				Ebean.find(Advertisement.class, id),
				CommentController.getList(id), commentForm));
	}

	public static Result ads(User u) {
		return ok(views.html.advertisement.ads.render(Advertisement
				.adsByUser(u)));
	}

	public static Result add() {
		if (UserController.getCurrentUser().getPriv("user")) {
			return ok(views.html.advertisement.upload.render(advertisementForm,
					Category.rootCategories()));
		}
		return redirect(controllers.user.routes.UserController.logIn());
	}

	public static Result upload() {

		Form<Advertisement> filledForm = advertisementForm.bindFromRequest();

		if (filledForm.hasErrors()) {
			return badRequest(views.html.advertisement.upload.render(
					filledForm, Category.find.all()));
		} else {

			Advertisement a = filledForm.get();

			a.setCategory(Ebean.find(Category.class,
					filledForm.data().get("category_id")));

			MultipartFormData body = request().body().asMultipartFormData();
			FilePart picture = body.getFile("photo");
			String fileName;
			// String contentType;
			if (picture == null) {
				a.setPhoto("example.png");
			} else {
				fileName = picture.getFilename();
				// contentType = picture.getContentType();
				File file = picture.getFile();
				file.renameTo(new File(Play.application().path()
						+ "\\public\\images\\advertisement\\" + fileName));
				a.setPhoto(fileName);
			}
			a.setUser(UserController.getCurrentUser());
			Advertisement.create(a);
			return redirect(controllers.advertisement.routes.AdvertisementController
					.list(a.getCategory().getId()));
			// return redirect(routes.Application.index());
		}
	}

	public static Result addComment(Long id) {
		Form<Comment> filledForm = commentForm.bindFromRequest();
		Comment c = new Comment();
		c.setText(filledForm.get().getText());
		c.setDate(new Date());
		c.setUser(UserController.getCurrentUser());
		c.setProduct(Ebean.find(Product.class, id));
		c.save();
		// return ok(views.html.index.render(c.toString()));
		return redirect(controllers.advertisement.routes.AdvertisementController
				.info(id));
	}

	public static Result delete(Long id) {
		Advertisement a = Ebean.find(Advertisement.class, id);
		Long cid = a.getCategory().getId();
		// TODO nem biztos hogz on delete set null kell mert jo ha torl=dik. on
		// delete cascade meg lehet torlni a usert.
		Advertisement.delete(id);

		return redirect(controllers.advertisement.routes.AdvertisementController
				.list(cid));
	}
}
